package client;
/*******************************************************************************
 *  Copyright (C) 2017 iQIYI Holdings, Inc.
 *  All Rights Reserved.
 *
 *  This file is part of cloud service. Unauthorized copy of this file, via any
 *  medium is strictly prohibited. Proprietary and Confidential.
 *
 *  Author(s):
 *      Xiaoshuang LU <luxiaoshuang@qiyi.com>
*******************************************************************************/



import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import iqiyi.solar.client.http.okhttp.OkhttpHTTPClient;
import iqiyi.solar.client.http.okhttp.OkhttpHTTPClientConfig;
import iqiyi.solar.common.SolarConstants;
import iqiyi.solar.common.configuration.ProfileType;
import iqiyi.solar.registry.zookeeper.ZookeeperDiscoveryClient;
import iqiyi.solar.registry.zookeeper.ZookeeperRegistryClient;
import iqiyi.solar.registry.zookeeper.ZookeeperRegistryConfig;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkhttpHTTPClient1 {
    private static final Logger LOGGER = LoggerFactory.getLogger(OkhttpHTTPClient1.class);

    private OkhttpHTTPClient solarClient;

    private AtomicBoolean flag = new AtomicBoolean(true);

    public OkhttpHTTPClient1(String serverAddress, int serverPort) {
    }

    
    public int open()  throws Exception  {
        this.solarClient = new OkhttpHTTPClient();

        OkhttpHTTPClientConfig clientConfig = new OkhttpHTTPClientConfig();
        clientConfig.setServerProjectID(String.valueOf(SolarConstants.SOLAR_PROJECT_ID));
        clientConfig.setClientProjectID(String.valueOf(SolarConstants.SOLAR_PROJECT_ID));
        clientConfig.setServiceName("HTTPExample");
        clientConfig.setProfile(ProfileType.PROD);

        this.solarClient.setConfig(clientConfig);

        ZookeeperRegistryConfig zookeeperDiscoveryConfig = new ZookeeperRegistryConfig();
        zookeeperDiscoveryConfig.setConnectionString(
            "solar-zookeeper-dev001-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev002-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev003-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev004-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev005-shgq.qiyi.virtual:2181");
        zookeeperDiscoveryConfig.setPath("/solar/test");
        zookeeperDiscoveryConfig.setServiceName(String.valueOf(SolarConstants.SOLAR_PROJECT_ID) + "/" + "HTTPExample");
        zookeeperDiscoveryConfig.setDiscoveryNamespace("servers");

        this.solarClient.setDiscovery(new ZookeeperDiscoveryClient(zookeeperDiscoveryConfig));

        ZookeeperRegistryConfig zookeeperRegistryConfig = new ZookeeperRegistryConfig();
        zookeeperRegistryConfig.setConnectionString(
            "solar-zookeeper-dev001-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev002-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev003-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev004-shgq.qiyi.virtual:2181," +
            "solar-zookeeper-dev005-shgq.qiyi.virtual:2181");
        zookeeperRegistryConfig.setPath("/solar/test");
        zookeeperRegistryConfig.setServiceName(String.valueOf(SolarConstants.SOLAR_PROJECT_ID) + "/" + "HTTPExample");
        zookeeperRegistryConfig.setRegistryNamespace("clients" + "/" + String.valueOf(SolarConstants.SOLAR_PROJECT_ID));

        this.solarClient.setRegistry(new ZookeeperRegistryClient(zookeeperRegistryConfig));
        this.solarClient.open();
        return 0;
    }

    public int run() throws Exception {
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();

        this.solarClient.attachMetricRegistry(okHttpClientBuilder);

        for (long i = 0; i < 64; ++ i) {
            try {
                OkHttpClient okHttpClient = okHttpClientBuilder.build();

                String url = "http://solar-kafka-kafka-dev001-shgq.qiyi.virtual:8080/solar-example-http-server-tomcat/test/0";

                Request.Builder requestBuilder = new Request.Builder();
                requestBuilder.url(url);

                Request request = requestBuilder.build();

                Response response = okHttpClient.newCall(request).execute();

                if (response.isSuccessful()) {
                    LOGGER.info(response.body().string());
                } else {
                    LOGGER.warn("failed.");
                }

                response.close();
            } catch (Exception e) {
                LOGGER.error("", e);
            }
        }

        return 0;
    }

    public int close() throws Exception {
        this.solarClient.close();

        if (! this.flag.get()) {
            throw new Exception();
        }

        return 0;
    }

    public static void main(String[] stringArray) {
        String serverAddress = null;
        Integer serverPort = null;
//        commandOptionParser commandOptionParser = new CommandOptionParser(stringArray);
//        commandOptionParser.parse();
//        serverAddress = commandOptionParser.getAddress();
//        serverPort = commandOptionParser.getPort();

        if (serverAddress == null ) {
            serverAddress = "localhost";
        }

        if (serverPort == null) {
            serverPort = 7171;
        }

        OkhttpHTTPClient1 client =
            new OkhttpHTTPClient1(
                serverAddress,
                serverPort);

        try {
            client.open();
            client.run();
            client.close();
        } catch (Exception e) {
            LOGGER.error("", e);
        }
    }
}
